package com.GraduationDesign.backend.controller.pullRequest;

import com.GraduationDesign.backend.bl.codeQuality.CodeQualityService;
import com.GraduationDesign.backend.bl.codeReview.ReviewService;
import com.GraduationDesign.backend.bl.git.GitService;
import com.GraduationDesign.backend.bl.project.ProjectService;
import com.GraduationDesign.backend.data.project.ProjectMapper;
import com.GraduationDesign.backend.po.DiffResponse;
import com.GraduationDesign.backend.po.Project;
import com.GraduationDesign.backend.vo.PRInfo;
import com.GraduationDesign.backend.vo.ResponseVO;
import lombok.val;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.GraduationDesign.backend.constant.Constants.DEFAULT_OUTPUT_DIR;
import static com.GraduationDesign.backend.constant.Constants.DEFAULT_PROJECT_LOCATION;


/**
 * @author: wzx
 * @date: 2022/2/13
 */
@RestController
@RequestMapping("/api/pull_request")
public class PullRequestController {
    @Autowired
    ProjectService projectService;
    @Autowired
    ReviewService reviewService;

    @Autowired
    CodeQualityService codeQualityService;
    @Autowired
    GitService gitService;
    @Autowired
    ProjectMapper projectMapper;

    @GetMapping("/get_pr_list")
    public ResponseVO getPRList(@RequestParam("programId") Integer programId){
        return ResponseVO.buildSuccess(projectService.getPrList(programId));
    }
    @PostMapping("/calculate")
    public DiffResponse calculate(@RequestBody PRInfo prInfo,@RequestParam("reviewId") Integer reviewId){
        return reviewService.calculate(prInfo,reviewId);
    }
    @PostMapping("/calculate_code_quality")
    @Deprecated
    public ResponseVO calculateCodeQuality(@RequestBody PRInfo prInfo,@RequestParam("reviewId") Integer reviewId){
        codeQualityService.calculateCodeQuality(prInfo, reviewId);
        return ResponseVO.buildSuccess();
    }
    @GetMapping("/get_file_tree")
    public ResponseVO getTree(@RequestParam("programId") Integer programId){
        gitService.pull(programId);
        Project projectInfo = projectMapper.getProjectInfo(programId);
        String path = DEFAULT_PROJECT_LOCATION + projectInfo.getRepositoryOwner()
                + "/" + projectInfo.getRepositoryName();
        return ResponseVO.buildSuccess(projectService.FileTree(path));
    }
    @GetMapping("/get_file")
    public ResponseVO getFile(@RequestParam("key") String path){
        return ResponseVO.buildSuccess(projectService.getFile(path));
    }
    @GetMapping("/pull")
    public ResponseVO pullFile(@Param("programId") Integer programId){
        gitService.pull(programId);
        return ResponseVO.buildSuccess();
    }

}
